/**
 * 
 */

package org.pexx.rdm.rift.collector;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import org.pexx.rdm.data.DataHandler;
import org.pexx.rdm.data.DataReceiver;
import org.pexx.rdm.data.DataSet;

/**
 * the datadispatcher receives every successfull parsed combatlog-line either from
 * local dataReader or from remote-dispatcher (using RMI)
 */
public class DataDispatcher extends UnicastRemoteObject implements DataHandler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5375571840683687103L;

	private DataReceiver localReceiver;

	private ArrayList<DataReceiver> receiver = new ArrayList<DataReceiver>();

	private String rmiName = "RIFTStats";

	/**
	 * @throws RemoteException
	 */
	public DataDispatcher() throws RemoteException {

		super();
	}

	/**
	 * @param localReceiver
	 *        the localReceiver to set
	 */
	public void setLocalReceiver(DataReceiver localReceiver) {

		this.localReceiver = localReceiver;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.pexx.rdm.data.DataReceiver#collect(org.pexx.rdm.data.DataSet)
	 */
	@Override
	public void collect(DataSet data) throws RemoteException {

		localReceiver.collect(data);

		for (DataReceiver c : receiver) {
			c.collect(data);
		}
	}

	@Override
	public void add(DataReceiver c) throws RemoteException {

		System.out.println("Adding Dispatcher " + c);
		receiver.add(c);
	}

	@Override
	public void remove(int i) {

		receiver.remove(i);
	}

	@Override
	public void remove(DataReceiver c) throws RemoteException {

		receiver.remove(c);
	}

	@Override
	public void clear() throws RemoteException {

		receiver.clear();
	}

	public void startServer() {

		if (System.getSecurityManager() == null)
			System.setSecurityManager(new RMISecurityManager());

		try {

			java.rmi.registry.LocateRegistry.createRegistry(1099);
			System.out.println("RMI registry ready.");
			
		} catch (Exception e) {
			System.out.println("Exception starting RMI registry:");
			//e.printStackTrace();
		}

		try {
			Naming.bind(rmiName, this);

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AlreadyBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 */
	public void stopServerMode() {

		try {
			Naming.unbind(rmiName);
			clear();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


	}

}
